#include<stdio.h> 
#include<QApplication>
#include<QDir>
#include<QPluginLoader>
#include<AlgorithmInterface.h>
#include<Visualizer.h>
#include<MainWindow.h>

// int main( int argc, char *argv[] ){
// 	QApplication app(argc, argv);
// 	
// 	printf("Hello world\n");
// 	
// 	QDir pluginsDir = QDir(qApp->applicationDirPath());
// 	pluginsDir.cd("plugins");
// 	
// 	foreach( QString fileName, pluginsDir.entryList(QDir::Files) ){
// 		QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
// 		loader.setLoadHints( QLibrary::LoadArchiveMemberHint | QLibrary::ExportExternalSymbolsHint | QLibrary::ResolveAllSymbolsHint );
// 		QObject *plugin = loader.instance();
// 		if( plugin ){
// 			AlgorithmInterface *i = qobject_cast<AlgorithmInterface*>(plugin);
// 			printf("%s is a plugin!!!\n", fileName.toAscii().data());
// // 			if(i)
// // 				i->run();
// // 			else
// // 				printf("Could not be cast\n");
// 		}else{
// 			printf("%s is not a plugin.\n", fileName.toAscii().data());
// 		}
// 	}
// 	
// 	return app.exec();
// }

int main( int argc, char *argv[] ){
	QApplication app(argc, argv);
	
	MainWindow *m = new MainWindow();
	m->show();
	
	QDir pluginsDir = QDir(QDir::currentPath());
	pluginsDir.cd("plugins");
	
	foreach( QString fileName, pluginsDir.entryList(QDir::Files) ){
		QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
		loader.setLoadHints( QLibrary::LoadArchiveMemberHint | QLibrary::ExportExternalSymbolsHint | QLibrary::ResolveAllSymbolsHint );
		QObject *plugin = loader.instance();
		if( plugin ){
			AlgorithmInterface *i = qobject_cast<AlgorithmInterface*>(plugin);
			printf("%s is a plugin!!!\n", fileName.toAscii().data());
			if(i)
				m->addAlgorithm(i);
		}
	}
	
	if( pluginsDir.cd("debug") ){
		foreach( QString fileName, pluginsDir.entryList(QDir::Files) ){
			QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
			loader.setLoadHints( QLibrary::LoadArchiveMemberHint | QLibrary::ExportExternalSymbolsHint | QLibrary::ResolveAllSymbolsHint );
			QObject *plugin = loader.instance();
			if( plugin ){
				AlgorithmInterface *i = qobject_cast<AlgorithmInterface*>(plugin);
				printf("%s is a plugin!!!\n", fileName.toAscii().data());
				if(i)
					m->addAlgorithm(i);
			}
		}
	}

	return app.exec();
}